home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / pwdmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  3.8 KB  |  171 lines

  1. /* UNIX password file access module */
  2.  
  3. #include "Python.h"
  4.  
  5. #include <sys/types.h>
  6. #include <pwd.h>
  7.  
  8. #ifdef AMITCP
  9. #include <proto/usergroup.h>
  10. #endif
  11. #ifdef INET225
  12. #include <proto/socket.h>
  13. #endif
  14.  
  15. #include "protos/pwdmodule.h"
  16.  
  17. static char pwd__doc__ [] = "\
  18. This module provides access to the Unix password database.\n\
  19. It is available on all Unix versions.\n\
  20. \n\
  21. Password database entries are reported as 7-tuples containing the following\n\
  22. items from the password database (see `<pwd.h>'), in order:\n\
  23. pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
  24. The uid and gid items are integers, all others are strings. An\n\
  25. exception is raised if the entry asked for cannot be found.";
  26.  
  27.       
  28. static PyObject *
  29. mkpwent(p)
  30.     struct passwd *p;
  31. {
  32. #ifdef __BEOS__
  33.     /* For faking the GECOS field. - [cjh] */
  34.     char *be_user = NULL;
  35.  
  36.     be_user = getenv( "USER" );
  37. #endif
  38.  
  39.     return Py_BuildValue(
  40.         "(ssllsss)",
  41.         p->pw_name,
  42.         p->pw_passwd,
  43. #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
  44. /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
  45.    for later versions you may have to remove this */
  46.         (long)p->pw_short_pad1,         /* ugh-NeXT broke the padding */
  47.         (long)p->pw_short_pad2,
  48. #else
  49.         (long)p->pw_uid,
  50.         (long)p->pw_gid,
  51. #endif
  52. #ifdef __BEOS__
  53. /* BeOS doesn't have a GECOS field, oddly enough. - [cjh] */
  54.         be_user ? be_user : "baron",
  55. #else
  56.         p->pw_gecos,
  57. #endif
  58.         p->pw_dir,
  59.         p->pw_shell);
  60. }
  61.  
  62. static char pwd_getpwuid__doc__[] = "\
  63. getpwuid(uid) -> entry\n\
  64. Return the password database entry for the given numeric user ID.\n\
  65. See pwd.__doc__ for more on password database entries.";
  66.  
  67. static PyObject *
  68. pwd_getpwuid(self, args)
  69.     PyObject *self;
  70.     PyObject *args;
  71. {
  72.     int uid;
  73.     struct passwd *p;
  74.     if (!PyArg_Parse(args, "i", &uid))
  75.         return NULL;
  76.     if ((p = getpwuid(uid)) == NULL) {
  77.         PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
  78.         return NULL;
  79.     }
  80.     return mkpwent(p);
  81. }
  82.  
  83. static char pwd_getpwnam__doc__[] = "\
  84. getpwnam(name) -> entry\n\
  85. Return the password database entry for the given user name.\n\
  86. See pwd.__doc__ for more on password database entries.";
  87.  
  88. static PyObject *
  89. pwd_getpwnam(self, args)
  90.     PyObject *self;
  91.     PyObject *args;
  92. {
  93.     char *name;
  94.     struct passwd *p;
  95.     if (!PyArg_Parse(args, "s", &name))
  96.         return NULL;
  97.     if ((p = getpwnam(name)) == NULL) {
  98.         PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
  99.         return NULL;
  100.     }
  101.     return mkpwent(p);
  102. }
  103.  
  104. #ifdef HAVE_GETPWENT
  105. static char pwd_getpwall__doc__[] = "\
  106. getpwall() -> list_of_entries\n\
  107. Return a list of all available password database entries, \
  108. in arbitrary order.\n\
  109. See pwd.__doc__ for more on password database entries.";
  110.  
  111. static PyObject *
  112. pwd_getpwall(self, args)
  113.     PyObject *self;
  114.     PyObject *args;
  115. {
  116.     PyObject *d;
  117.     struct passwd *p;
  118.     if (!PyArg_NoArgs(args))
  119.         return NULL;
  120.     if ((d = PyList_New(0)) == NULL)
  121.         return NULL;
  122. #if !defined(AMITCP) && !defined(INET225)
  123.     setpwent();
  124.     while ((p = getpwent()) != NULL) {
  125.         PyObject *v = mkpwent(p);
  126.         if (v == NULL || PyList_Append(d, v) != 0) {
  127.             Py_XDECREF(v);
  128.             Py_DECREF(d);
  129.             return NULL;
  130.         }
  131.         Py_DECREF(v);
  132.     }
  133.     return d;
  134. #else
  135.  #ifdef AMITCP
  136.     setpwent();
  137.  #else 
  138.     setpwent(1); /* INET225 wants argument XXX correct? - I.J. */
  139.  #endif
  140.     while ((p = getpwent()) != NULL) {
  141.         PyObject *v = mkpwent(p);
  142.         if (v == NULL || PyList_Append(d, v) != 0) {
  143.             Py_XDECREF(v);
  144.             Py_DECREF(d);
  145.             endpwent();
  146.             return NULL;
  147.         }
  148.         Py_DECREF(v);
  149.     }
  150.     endpwent();
  151.     return d;
  152. #endif /* AMITCP or INET225 */
  153. }
  154. #endif
  155.  
  156. static PyMethodDef pwd_methods[] = {
  157.     {"getpwuid",    pwd_getpwuid, 0, pwd_getpwuid__doc__},
  158.     {"getpwnam",    pwd_getpwnam, 0, pwd_getpwnam__doc__},
  159. #ifdef HAVE_GETPWENT
  160.     {"getpwall",    pwd_getpwall, 0, pwd_getpwall__doc__},
  161. #endif
  162.     {NULL,        NULL}        /* sentinel */
  163. };
  164.  
  165. DL_EXPORT(void)
  166. initpwd()
  167. {
  168.     Py_InitModule4("pwd", pwd_methods, pwd__doc__,
  169.                        (PyObject *)NULL, PYTHON_API_VERSION);
  170. }
  171.